from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from utils.sorter import paging
from .models import Path,Stage
from course.models import Course
from course.serializers import CourseSer
from .serializers import PathSer,StageSer
# Create your views here.

# 展示所有路径
class Show_Path_View(APIView):


    def get(self,request):

        # 接收当前页
        page = int(request.GET.get('page',1))
        # 接收一页的数据个数
        size = int(request.GET.get('size',4))
        sorter = paging(page,size)
        # 查询数据并切割
        paths = Path.objects.all()
        path = paths[sorter[0]:sorter[-1]]
        # 序列化
        pathser = PathSer(path,many=True)
        # 数据总数
        count = paths.count()

        return Response({'code':200,'msg':'路径数据','data':pathser.data,'total':count})

# 展示所有阶段节点
class Show_Stage_View(APIView):

    def get(self,request):

        # 接收路径id
        path_id = request.GET.get('path_id')

        # 关联查询
        stage = Stage.objects.filter(path_id=path_id)

        stage_list = []

        for stages in stage:

            stage_list.append(stages.id)

        # 查询有关于路径的所有课程
        course = Course.objects.filter(stage_id__in=stage_list)

        # 序列化节点
        stageser = StageSer(stage,many=True)
        # 序列化节点下的课程
        courseser = CourseSer(course,many=True)


        return Response({'code':200,'msg':'对应路径的阶段节点数据','stage':stageser.data,'course':courseser.data})